﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Cars
{
    public class Cars
    {
        public class Brand
        {
            protected string _brandName;
            protected string _country;

            public string BrandName
            {
                get { return _brandName; }
                set { _brandName = value; }
            }

            public string Country
            {
                get { return _country; }
                set { _country = value; }
            }
        }

        public class Model : Engine
        {
            protected string _modelName;
            protected int _startYear;
            protected int _price;

            public string ModelName
            {
                get { return _modelName; }
                set { _modelName = value; }
            }

            public int StartYear
            {
                get { return _startYear; }
                set { _startYear = value; }
            }

            public int Price
            {
                get { return _price; }
                set { _price = value; }
            }

        }

        public class Engine : Brand
        {
            protected int _volume;
            protected string _gasoline;
            protected int _power;
            protected int _fuelEcon;

            public int FuelEcon
            {
                get { return _fuelEcon; }
                set { _fuelEcon = value; }
            }

            public string Gasoline
            {
                get { return _gasoline; }
                set { _gasoline = value; }
            }

            public int Volume
            {
                get { return _volume; }
                set { _volume = value; }
            }

            public int Power
            {
                get { return _power; }
                set { _power = value; }
            }
        }

        public class ProductFactory
        {
            static public Model CreateModel(int type, string brandName, string country, string modelName = "", int year = 0, int price = 0, int volume = 0, int power = 0, int fuelEcon = 0, string gasoline = "gasoline")
            {
                Model car = new Model();
                ILogger log = LoggerFactory.CreateLogger(0);

                car.BrandName = brandName;
                car.Country = country;

                if (type > 0)
                {
                    car.ModelName = modelName;
                    car.StartYear = year;
                    car.Price = price;
                }

                if (type > 1)
                {
                    car.Volume = volume;
                    car.Power = power;
                    car.FuelEcon = fuelEcon;
                    car.Gasoline = gasoline;
                }

                switch (type)
                {
                    case 0:
                        log.Log("Экземпляр типа 'Brand' создан");
                        break;
                    case 1:
                        log.Log("Экземпляр типа 'Model' создан");
                        break;
                    case 2:
                        log.Log("Экземпляр типа 'Engine' создан");
                        break;
                }

                return car;
            }
        }

        public class CalculatePrice
        {
            public int GetTotalPrice(Model[] car, int value)
            {
                ILogger log = LoggerFactory.CreateLogger(0);

                int totalPrice = 0;

                for (int i = 0; i < value; ++i)
                {
                    totalPrice += car[i].Price;
                    log.Log("Цена автомобиля №" + i + " " + car[i].Price + "добавлена. Общая стоимость: " + totalPrice);
                }

                return totalPrice;
            }
        }


        public interface ILogger
        {
            void Log(string message);
        }

        public class ConsoleLogger : ILogger
        {
            public void Log(string message)
            {
                Console.WriteLine("[LOG] " + message);
            }
        }

        public class FileLogger : ILogger
        {
            public void Log(string message)
            {
                string path = "log.txt";

                if (!File.Exists(path))
                {
                    using (StreamWriter sw = File.CreateText(path))
                    {
                        sw.WriteLine(message);
                    }
                }
                else
                {
                    using (StreamWriter sw = File.AppendText(path))
                    {
                        sw.WriteLine(message);
                    }
                }
            }
        }

        public class LoggerFactory
        {
            static public ILogger CreateLogger(int type)
            {
                if (type == 0)
                {
                    ConsoleLogger log = new ConsoleLogger();
                    return log;
                }
                else
                {
                    FileLogger log = new FileLogger();
                    return log;
                }
            }
        }

        public class CarPrinter
        {
            public void Print(string text, int value)
            {
                Console.WriteLine(text + ": " + value);
            }
        }


    }
}